package com.sunyur.tailor.channel.web.outer.receiveorder.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.receiveorder.param.CreateReceiveOrderParam;
import com.sunyur.tailor.channel.service.inner.receiveorder.service.SunyurReceiveOrderService;
import com.sunyur.tailor.channel.web.outer.receiveorder.dto.CreateReceiveOrderDTO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author pengbing
 * @Description //创建收货单
 * @Date 17:05 2021/12/30
 * @Param
 * @return
 **/
@Log4j2
@RestController
@RequestMapping("/outer/receiveOrder")
public class ReceiveOrderController {
    /**
     *收货单服务
     */
    @Autowired
    private SunyurReceiveOrderService sunyurReceiveOrderService;

    /**
     *创建收货单
     */
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult create(@RequestBody @Valid CreateReceiveOrderDTO createReceiveOrderDTO) {
        log.info("ReceiveOrderController create param:{}", JSON.toJSONString(createReceiveOrderDTO));
        ChannelResult result = new ChannelResult<>();
        if (Objects.isNull(createReceiveOrderDTO)) {
            log.error("ReceiveOrderController create param is null");
            result.setError(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
            return result;
        }
        try {
            CreateReceiveOrderParam createReceiveOrderParam = new CreateReceiveOrderParam();
            BeanUtils.copyProperties(createReceiveOrderDTO,createReceiveOrderParam);
            result = sunyurReceiveOrderService.createReceiveOrder(createReceiveOrderParam);
        } catch (Exception e) {
            log.error("ReceiveOrderController create exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }
}